package chap8;
//******************************************************************
//  PP8_5.java                  Author:GK
//
//  计算并输出一组整数 x1~xn 的平均值和方差。输入不超过50个值，
//  以浮点数计算均值（mean）和标准方差（sd）。
//******************************************************************
import java.util.*;

public class PP8_5 {
    public static void main(String[] args) {
        double sum = 0, sum2 = 0;
        int x;
        Scanner scan = new Scanner(System.in);

        System.out.print("输入多少个值？(不超过50个)：");
        x = scan.nextInt();

        double[] numbers = new double[x];
        double[] numbers2 = new double[x];

        if (x > 50)
            System.out.println("不能超过50！");
        else {
            for (int times = 1;times <= x; times++ )
            {
                int a;
                int b = 0;
                System.out.print("输入x" + times + "数字: ");
                a = scan.nextInt();
                numbers[times-1] = a;
            }

            System.out.println();
            for (int p = 0; p <= x-1; p++)
            {
                sum +=numbers[p];
            }

            double mean;
            mean = sum/x;

            for (int times = 1; times <= x; times++)
            {
                double a,b;
                a = numbers[times-1]-mean;
                b = Math.pow(a,2);
                numbers2[times-1] = b;
            }

            for (int p = 0; p <= x-1; p++)
            {
                sum2 +=numbers2[p];
            }
            double sd;
            sd = Math.sqrt(sum2);

            System.out.println("均值为： " + mean);
            System.out.println("标准方差为：" + sd);
        }
    }
}
